<?php

namespace app\admin\etf\controller;

use app\admin\etf\model\EtfCodeDay;
use app\common\services\EtfService;
use support\Request;
use support\Response;
use app\admin\etf\model\EtfLists;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;

/**
 * 基础数据 
 */
class EtfListController extends Crud
{
    
    /**
     * @var EtfLists
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new EtfLists;
    }
    
    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('etf/etf-list/index');
    }

    // 查询后置操作
    public function afterQuery($items)
    {
        foreach ($items as $k=>$v){
            $price = EtfCodeDay::where(['code'=>$v->code])->orderBy('id','desc')->value('close');
            $items[$k]['price'] = number_format($price,3);
            $items[$k]['update_at'] = substr($v->update_at,0,10);
            $items[$k]['average'] = EtfService::getAverage($v->code);
        }
        return $items; // TODO: Change the autogenerated stub
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::insert($request);
        }
        return view('etf/etf-list/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
    */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::update($request);
        }
        return view('etf/etf-list/update');
    }

}
